package cn.edu.cqut.controller;


import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.edu.cqut.entity.DivideModel;
import cn.edu.cqut.entity.Orders;
import cn.edu.cqut.service.IOrdersService;
import cn.edu.cqut.util.PageResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HQYJ
 * @since 2020-06-24
 */
@RestController
@RequestMapping("/orders")
@CrossOrigin  //允许跨域请求
public class OrdersController {
	@Autowired
	private IOrdersService ordersService;
	
	@RequestMapping("/getOrdersList")
	public PageResult<Orders> getOrdersrList(
			@RequestParam(defaultValue = "1") Integer page, //@RequestParam给参数设置默认值
			@RequestParam(defaultValue = "10") Integer limit,
			Orders orders){
		QueryWrapper<Orders> queryWapper = new QueryWrapper<>();
		Page<Orders> page3 = new Page<Orders>(page,limit);
		Page<Orders> page2 = null;
		System.out.println(orders.getCusName());
		if(orders.getCusName() != null && orders.getCusName() != "") {
			//模糊查询条件，第一个参数是表的字段名，不是实体的属性名
			queryWapper.eq("cus_Name", orders.getCusName());
		}

		if (orders.getOrDate() != null) {
			SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
			page2 =  ordersService.getOrdersByDate(page3, yearFormat.format(orders.getOrDate()));
		}
		else {
		    page2 = ordersService.page(page3, queryWapper);
		}
		PageResult<Orders> pr = new PageResult<>();
		
		long count = page2.getTotal();
		pr.setCount(count);
		pr.setCode(0);
		pr.setMsg("");
		pr.setData(page2.getRecords());
		return pr;
	}
	
	@RequestMapping("/getDrawOptions")
	public Map<String , List<Object>> getDrawOptions(String searchInfo, Boolean isNumber) {
		// 如果isNumber为true说明查询条件是年份
		Map<String, List<Object>> ret = new HashMap<>();
		if (!isNumber) {
			List<DivideModel> orders = ordersService.getOrdersByCus_Name(searchInfo);
			List<Object> xList = new ArrayList<>();
			List<Object> yList = new ArrayList<>();
			List<Object> lineList = new ArrayList<>();
			for (DivideModel rm : orders) {
				xList.add(rm.getName()); // 日期
				yList.add((float)rm.getValue()/(float)10000); // 总金额
				lineList.add((float)rm.getValue()/(float)10000 + 1); // 折线图数据 
			}
			ret.put("x", xList);
			ret.put("y", yList);
			ret.put("line", lineList);
		}
		else {
			List<DivideModel> orders = ordersService.getOrderByDate(searchInfo);
			List<Object> xList = new ArrayList<>();
			List<Object> yList = new ArrayList<>();
			
			for (DivideModel rm : orders) {
				System.out.println(rm.getValue());
				xList.add(rm.getName()); // 日期
				if (rm.getValue() != null) {
					yList.add((float)rm.getValue()/(float)10000); // 总金额
				}	
			}
			ret.put("x", xList);
			ret.put("y", yList);
		}
		return ret;
	}
	 @RequestMapping("/delorder")  //添加前端映射
	    public PageResult<Orders> delorder(Integer[] ids){
	        ordersService.removeByIds(Arrays.asList(ids));  //数组转list
	        System.out.println(ids);
	        PageResult<Orders> pr = new PageResult<>();
	        pr.setMsg("删除订单成功！");
	        pr.setCode(0);
	        return pr;
	    }
	    @RequestMapping("/addorders")
	    public PageResult<Orders> addgetorders(Orders orders) {
	        ordersService.save(orders);
	        PageResult<Orders> pr = new PageResult<>();
	        pr.setCode(0);
	        pr.setMsg("新增订单成功！");
	        return pr;
	        // page分页对象  getRecords得到list列表
	    }
	    @RequestMapping("/getorders")
	    public PageResult<Orders> getorderslist(
	            @RequestParam(defaultValue = "1") Integer page,//RequestParam设置默认请求
	            @RequestParam(defaultValue = "10") Integer limit,
	            Orders orders) {
	        QueryWrapper<Orders> queryWapper = new QueryWrapper<>();
	        if(orders.getCusName()!= null) {
	            // 模糊查询条件： 第一个是表里面的一个字段名
	            queryWapper.like("cus_name", orders.getCusName());
	        }
	        Page<Orders> page3 = new Page<Orders>(page,limit);
	        Page<Orders> page2 = ordersService.page(page3,queryWapper);
	        PageResult<Orders> pr = new PageResult<>();
	        pr.setCode(0);
	        pr.setCount(page2.getTotal()); //总数
	        pr.setMsg("查询订单");
	        pr.setData(page2.getRecords());
	        return pr;
	        // page分页对象  getRecords得到list列表
	    }
}

